import { Component, OnInit, Input } from '@angular/core';
import { SearchListService } from 'src/app/shared/service/search-list.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { ModalController } from '@ionic/angular';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';

@Component({
  selector: 'app-choose-department',
  templateUrl: './choose-department.component.html',
  styleUrls: ['./choose-department.component.scss'],
})
export class ChooseDepartmentComponent implements OnInit {

  @Input() tagData: any;
  @Input() modalTitle = '选择部门';
  @Input() backValue: any;
  treeData = []; // 数据

  subscription: any; // 订阅对象

  isSearch = false; // 显示搜索框

  isHistory = false; // 显示搜索历史

  key: any; // 搜索值

  searchArr = []; // 搜索历史

  pageData: any; // 页面数据

  @Input() value = null; // 选中值

  httpData = {
    id: '',
    parentCode: ''
  }; // 数据请求所需参数


  constructor(
    private appGlobalService: AppGlobalService,
    private modalController: ModalController,
    private broadcast: SearchListService,
    private http: HttpService
  ) { }

  ngOnInit() {

    const pageParams = {
      pageId: this.tagData.target,
      params: this.tagData.params,
      sessionId: localStorage.getItem("sessionId")
    };
    // 获取归属部门页面
    this.http.post('application/page', pageParams, res => {
      if (res !== undefined) {
        this.pageData = JSON.parse(res.script).views[0].tags[0];
        this.treeData = this.pageData.treeData;
        this.httpData.id = this.pageData.id;
        this.httpData.parentCode = this.pageData.parentCode;
      }
    });
    this.subscription = this.broadcast.getMessage().subscribe(res => {
      if (res.type === "submit" && this.tagData.submitId === res.id) {
        if (res.value.data) {
          this.treeData = res.value.data.data;
        }
      }
    });
  }

  //  关闭弹框
  doDiss() {
    this.modalController.dismiss();
  }

  //  清空搜索历史
  clearHistory() {
    this.searchArr = [];
    localStorage.removeItem('SearchdepHistory');
  }

  // 历史搜索
  searchHistory(value) {
    this.key = value;
    this.search();
  }

  // 搜索部门
  search() {
    // this.isSearch = false;
    if (!this.key) {
      return;
    }
    const params = {
      content: {
      },
      id: this.pageData.searchBar.id,
      sessionId: localStorage.getItem('sessionId')
    };
    params.content[this.pageData.searchBar.name] = this.key;
    const len = this.searchArr.length;
    if (this.key !== "" && this.searchArr.indexOf(this.key) === -1) {
      this.searchArr.unshift(this.key);
      if (len >= 10) {
        this.searchArr.pop();
      }
      localStorage.setItem('SearchdepHistory', JSON.stringify(this.searchArr));
    }
    
    // 请求数据
    this.http.post('application/submit', params, (res) => {
      this.key = null;
      this.isHistory = false;
      if (res.success) {
        if (res.data.data) {
          this.treeData = res.data.data;
        } else {
          this.treeData = [];
        }
      } else {
        this.appGlobalService.showToast(res.message, 1500, "top");
      }


    });
  }

  //  确定
  submit() {
    if (!this.value) {
      this.modalController.dismiss(null);
      return;
    }
    if (this.pageData.button[0].submit && this.backValue !== true) {
      const params = {
        content: {
        },
        id: this.pageData.button[0].submit.id,
        sessionId: localStorage.getItem('sessionId')
      };
      for (const key in this.pageData.button[0].submit.params) {
        if (this.pageData.button[0].submit.params[key]) {
          params.content[key] = this.pageData.button[0].submit.params[key];
        } else {
          params.content[key] = this.value.code;
        }
      }
  
      this.http.post('application/submit', params, (res) => {
        console.log(res);
        if (res.success) {
          this.modalController.dismiss(this.value);
        } else {
          this.appGlobalService.showToast(res.message, 1500, "top");
        }
      });
    } else {
      this.modalController.dismiss(this.value);
    }
   
  }


  // 展示按钮
  clk() {
    this.isSearch = true;
    this.isHistory = true;
    if (localStorage.getItem('SearchdepHistory')) {
      this.searchArr = JSON.parse(localStorage.getItem('SearchdepHistory'));
    }
  }

  onValue(item: any) {
    if (!item) {
      return;
    } else {
      this.value = item;
    }
  }

}
